package com.ymatou.userregister.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.parameter.WeiXinUserRegBean;
import com.ymatou.userregister.service.WeiXinUserRegCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

public class Ts_WeiXinUserReg {
	private static WeiXinUserRegBean regBean;
	private static WeiXinUserRegCall regCall;
	private YmtUsersWapper userWapper;
	private UserTokenWapper userTokenWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("微信用户注册 ");
	}

	@Before
	public void caseUp() throws Exception {
		regBean = new WeiXinUserRegBean();
		regCall = new WeiXinUserRegCall();
		userWapper = new YmtUsersWapper();
		userTokenWapper = new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(regCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiXinUserReg_001() {
		Logger.start(true,"创建用户成功");
		try {
			String openId = "openId_"+TestDataManager.getRandomNum(4);
			regBean.setCountry("China");
			regBean.setProvince("Shanghai");
			regBean.setCity("Shanghai");
			regBean.setClientIp("1.1.1.1");
			regBean.setHeadimgurl("");
			regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
			regBean.setOId(openId);
			regBean.setRegSource("mainsite");
			regBean.setSex("Femail");
			regBean.setTimeOut(new Date().getTime());
			regBean.setTSource("Pc");
			regBean.setUnionId("unionId_"+TestDataManager.getRandomNum(4));
			regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(4));
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("注册成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Map user = userWapper.selectUserAtWeiXinByOpenId(openId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_WeiXinUserReg_002() {
		Logger.start(true,"输入已关联过的用户(openId和UnionId已存在) ，则用户直接登陆成功");
		try {
			Map user = userWapper.selectUserAtWeixinByUserId(20229428);
			
			regBean.setCountry("China");
			regBean.setProvince("Shanghai");
			regBean.setCity("Shanghai");
			regBean.setClientIp("1.1.1.1");
			regBean.setHeadimgurl("");
			regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
			regBean.setOId(user.get("sOpenId").toString());
			regBean.setRegSource("mainsite");
			regBean.setSex("Femail");
			regBean.setTimeOut(new Date().getTime());
			regBean.setTSource("Pc");
			regBean.setUnionId(user.get("sUnionId").toString());
			regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(4));
			regCall.setData(regBean);
			regCall.callService();	
			
			Logger.verifyEquals("登录成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Map userToken = userTokenWapper.getUserTokenByUid(20229428);
			
			Logger.verifyEquals(userToken.get("uId"), regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserReg_003() {
		Logger.start(false,"不输入Open Id，用户创建失败");
		try {
			regBean.setCountry("China");
			regBean.setProvince("Shanghai");
			regBean.setCity("Shanghai");
			regBean.setClientIp("1.1.1.1");
			regBean.setHeadimgurl("");
			regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
			regBean.setRegSource("mainsite");
			regBean.setSex("Femail");
			regBean.setTimeOut(new Date().getTime());
			regBean.setTSource("Pc");
			regBean.setUnionId("unionId_"+TestDataManager.getRandomNum(4));
			regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(4));
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("false", regCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals(ECode.ERROR400, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("dto验证失败", regCall.getString("LastErrorMessage"), "验证返回的Message信息");
			Logger.verifyEquals(-1d, regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyIsNull(regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals(31d, regCall.getResult().get("VerifyCode"), "验证凭据(31表示微信OpenId不能为空)");
			Logger.verifyIsNull(regCall.getResult().get("Remark"), "验证remark信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserReg_004(){
		Logger.start(true,"不输入微信Token，注册成功");
		try {
			String openId = "openId_"+TestDataManager.getRandomNum(4);
			regBean.setCountry("China");
			regBean.setProvince("Shanghai");
			regBean.setCity("Shanghai");
			regBean.setClientIp("1.1.1.1");
			regBean.setHeadimgurl("");
			regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
			regBean.setOId(openId);
			regBean.setRegSource("mainsite");
			regBean.setSex("Femail");
			regBean.setTimeOut(new Date().getTime());
			regBean.setTSource("Pc");
			regBean.setUnionId("unionId_"+TestDataManager.getRandomNum(4));
			regCall.setData(regBean);
			regCall.callService();	
			
			Logger.verifyEquals("注册成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Map user = userWapper.selectUserAtWeiXinByOpenId(openId);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_WeiXinUserReg_005(){
		Logger.start(true,"不输入UnionId，用户创建成功");
		try {
			String openId = "openId_"+TestDataManager.getRandomNum(4);
			regBean.setCountry("China");
			regBean.setProvince("Shanghai");
			regBean.setCity("Shanghai");
			regBean.setClientIp("1.1.1.1");
			regBean.setHeadimgurl("");
			regBean.setName("WeiXiUser_"+TestDataManager.getRandomNum(5));
			regBean.setOId(openId);
			regBean.setRegSource("mainsite");
			regBean.setSex("Femail");
			regBean.setTimeOut(new Date().getTime());
			regBean.setTSource("Pc");
			regBean.setWeixinToken("weixintoken_"+TestDataManager.getRandomNum(4));
			
			regCall.setData(regBean);
			regCall.callService();			
			Logger.verifyEquals("注册成功", regCall.getString("SuccessMessage"), "验证返回的Message信息");
			Logger.verifyEquals(ECode.SUCCESS, regCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", regCall.getString("Success"), "验证是否成功");
			Map user = userWapper.selectUserAtWeiXinByOpenId(openId);
			Thread.sleep(1000);
			int userId = (Integer)user.get("iYmtUserId");
			Map userToken = userTokenWapper.getUserTokenByUid(userId);
			
			Logger.verifyEquals(userToken.get("uId"), regCall.getResult().get("UserId"), "验证用户ID");
			Logger.verifyEquals(userToken.get("sId"), regCall.getResult().get("Token"), "验证Token信息");
			Logger.verifyEquals("new", regCall.getResult().get("Remark"), "验证Remark信息");
			Logger.verifyEquals(14d, regCall.getResult().get("VerifyCode"), "验证凭据(14表示操作成功)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
